<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<pre>
<?php
$arrAfter = unserialize(gzuncompress(base64_decode($_POST['postdata']))); 
//print_r($arrAfter);
//print_r($_POST);
//1 read template 
$templateFileName="tp-8.html";
$lpp=14;
$fl=fopen($templateFileName,"r");
$initString = fread($fl,filesize($templateFileName));
fclose($fl);
$rBegin = $_POST['rangebegin'];
$rEnd = $_POST['rangeend'];
$totalLabel = $_POST['totallabel'];
$counter = 1;
$pageCounter = 1;
$qtCounter = 1;
$qtI=0;
$totalPage=1;
$linkString = "";
if($totalLabel>$lpp){
echo $totalPage = ($totalLabel-$totalLabel%$lpp)/$lpp +1;
}
$ordrCounterBegin="";
$ordrCounterEnd = "";
for($i=$rBegin;$i<$rEnd+1; $i++){
	
	if($counter==1 or ($counter-1)%$lpp==0){
	$pageString = $initString;	
	$ordrCounterEnd = "";
	$ordrCounterBegin = $arrAfter['orderNO'.$i];
	
	if($pageCounter==$totalPage){
		for($looper=0;$looper<$lpp;$looper++){
			if(isset($_POST['position'.$looper])){
				$pageString=str_replace_once("[R0P0L0A0C]","#R0P0L0A0C#",$pageString);
				}
			else{
				$pageString=str_replace_once("[R0P0L0A0C]"," ",$pageString);
				}	
			
			}
		$pageString=str_replace("#R0P0L0A0C#","[R0P0L0A0C]",$pageString);
		}
	}
	
	if(isset($arrAfter['choice'.$i])){
		
			
			$cellString = buildCell($arrAfter['orderNO'.$i],$arrAfter['userName'.$i],$arrAfter['userID'.$i],$arrAfter['addrLine1'.$i],$arrAfter['addrLine2'.$i],$arrAfter['addrCity'.$i],$arrAfter['addrState'.$i],$arrAfter['postCode'.$i],$arrAfter['qty'.$i],$arrAfter['postType'.$i]);
			
			$pageString = str_replace_once("[R0P0L0A0C]",$cellString,$pageString);
			
			$counter++;
			
			if($arrAfter['qt'.$i] - substr_count($pageString,$cellString)){$i--;}
			
			

		}	
		if(strpos($pageString,"[R0P0L0A0C]")=== false){
			$ordrCounterEnd=$arrAfter['orderNO'.$i];
			$folderToday=strval(date("Y-m-d"));
			if(!file_exists($folderToday)){
			mkdir($folderToday);
			}
			$htmlFileName=$ordrCounterBegin."-".$ordrCounterEnd.".html";
			$fl=fopen($folderToday."/".$htmlFileName,"w");
			fwrite($fl,$pageString);
			fclose($fl); 
			$counter=1;
			$linkString .= "<br /><a href=\"$folderToday/$htmlFileName\">$htmlFileName</a><br />";
			$pageCounter++;
			}
			



	} 
echo $linkString;	

function buildCell($orderNO,$userName,$userID,$addrLine1,$addrLine2,$addrCity,$addrState,$postCode,$qtyStr){
	//var_dump($qtyStr);
 $blockstring = "
 <b>#$orderNO Post To:</b><img src='http://192.168.1.124/sample/sample-gd-26.php?sno=$orderNO' border='0' style=\"height:16px;width:170px;\" /> <br />
$userName<span style=\"font-size:10px\">($userID)</span><br />
<b>$addrLine1</b><br />
$addrLine2<br />
$addrCity<br />
$addrState &nbsp;<br />
<div class=\"thepostcode\">$postCode</div>
<span style=\"border-top: solid 1px #000000; \">$qtyStr</span> <br />
 ";	
//echo $blockstring;	
return $blockstring;	
	}
    function str_replace_once($str_pattern, $str_replacement, $string){
       
        if (strpos($string, $str_pattern) !== false){
            $occurrence = strpos($string, $str_pattern);
            return substr_replace($string, $str_replacement, strpos($string, $str_pattern), strlen($str_pattern));
        }
       
        return $string;
    } 
?>
</pre>
</body>
</html>